package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.ContactDao;
import com.aaa.vegetablemarketexpress.entity.Contact;
import com.aaa.vegetablemarketexpress.service.ContactService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.PageResult;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: ContactServiceImpl
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/22 19:49
 * @Version: 1.0.0
 */
@Service
public class ContactServiceImpl implements ContactService {

    @Resource
    private ContactDao contactDao;

    /**
     * 用户提交咨询信息
     * @param message
     * @return
     */

    @Override
    public int submitMessage(Contact message) {
        // 设置默认状态为未回复
        message.setStatus(0);
        return contactDao.insert(message);
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @Override
    public PageInfo<Contact> findByPage(Page<Contact> page) {
        // 验证分页参数
        if (page.getPageNo() == null || page.getPageSize() == null
                || page.getPageNo() < 1 || page.getPageSize() < 1) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }

        // 开启分页
        PageHelper.startPage(page.getPageNo(), page.getPageSize());

        // 执行查询
        List<Contact> contactList = contactDao.selectByParam(page.getData());

        // 封装分页结果
        return new PageInfo<>(contactList);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Contact selectById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("咨询ID不能为空");
        }
        return contactDao.selectById(id);
    }

    /**
     * 回复咨询
     * @param id
     * @param replyContent
     * @param adminId
     * @return
     */
    @Override
    public int replyMessage(Long id, String replyContent, Long adminId) {
        if (id == null || replyContent == null || replyContent.trim().isEmpty() || adminId == null) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }

        // 验证咨询是否存在
        Contact contact = contactDao.selectById(id);
        if (contact == null) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), "咨询不存在");
        }

        // 验证是否已回复
        if (contact.getStatus() != null && contact.getStatus() == 1) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), "该咨询已回复");
        }

        // 构建更新对象
        Contact updateContact = new Contact();
        updateContact.setId(id);
        updateContact.setReplyContent(replyContent);
        updateContact.setReplyAdminId(adminId);

        // 执行更新
        return contactDao.updateReply(updateContact);
    }

    /**
     * 根据用户ID查询历史咨询记录
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<Contact> getByUserId(Long userId) {
        return contactDao.getByUserId(userId);
    }
}
